package com.spdg.ring;

import java.util.List;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import cn.wolf.tools.MathUtil;
import cn.wolf.tools.StringUtil;

import com.spdg.ring.offline.DownloadException;
import com.spdg.ring.offline.DownloadListener;
import com.spdg.ring.offline.OfflineDownloadManager;
import com.spdg.ring.offline.Progress;

/**
 * <p>Title: 离线下载界面 </p>
 * <p>Description:  </p>
 * <p>@author: caijj                </p>
 * <p>Copyright: Copyright (c) 2012    </p>
 * <p>Company: HX Co., Ltd.          </p>
 * <p>Create Time: 2012-11-17             </p>
 * <p>Update Time:                     </p>
 * <p>Updater:                         </p>
 * <p>Update Comments:                 </p>
 */
public class ZipDownloadActivity extends RingBaseActivity {

	TextView progressDesc;
	ProgressBar progressBar;

	Button mStop; // 取消下载

	@Override
	protected void initComponents() {

		boolean success = OfflineDownloadManager.getInstance().loadSuccess();
		if (success) {
			OfflineDownloadManager.getInstance().clearNotify();
			finish();
		}

		progressBar = (ProgressBar) findViewById(R.id.offline_progress);
		progressDesc = (TextView) findViewById(R.id.offline_desc);

		mStop = (Button) findViewById(R.id.offline_stop);
		mStop.setOnClickListener(new OnStopListener());
	}

	@Override
	protected void initData() {
		OfflineDownloadManager.getInstance().addDownloadListener(listener);
	}

	private void updateProgress(final Progress p) {
		if (p != null) {
			runOnUiThread(new Runnable() {

				@Override
				public void run() {
					progressBar.setMax(p.total);
					progressBar.setProgress(p.complete);
				}
			});
		}
	}

	private void updateProgressDesc(final String format) {
		if (!StringUtil.isEmpty(format)) {
			runOnUiThread(new Runnable() {

				@Override
				public void run() {

					progressDesc.setText(format);
				}
			});
		}
	}

	private void updateButton() {
		mStop.setText(getString(R.string.offline_progress_success));
	}

	class OnStopListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			OfflineDownloadManager.getInstance().cancel();
			finish();
		}
	}

	DownloadListener listener = new DownloadListener() {

		@Override
		public void onProgress(Progress progress) {
			updateProgress(progress);

			String format = "";
			format = mContext.getString(R.string.offline_progress_desc);
			format = String.format(format, progress.name,
					MathUtil.percent(progress.total, progress.complete));
			updateProgressDesc(format);
		}

		@Override
		public void onPostExecute(List<Progress> list) {
			updateButton();
		}

		@Override
		public void onError(DownloadException e) {
			Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show();
			updateProgressDesc(getString(R.string.offline_load_error_ioexception));
		}

		@Override
		public void onPostProgress(int index, Progress progress) {

		}
	};

	@Override
	protected void onPause() {
		super.onPause();
		OfflineDownloadManager.getInstance().removeDownloadListener(listener);
	}

	@Override
	protected int getMainContentViewId() {
		return R.layout.act_offline_download;
	}

	@Override
	protected Class<?> getResouceClass() {
		return R.class;
	}

	@Override
	protected String getEventID() {
		return null;
	}

}
